<template>
  <div class="container">
    <div class="container">
      <el-dialog title="修改标签" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" :rules="rules" ref="dataForm">
          <el-form-item label="所属学科" prop="subjectID" v-if="hehe">
            <el-select v-model="form.subjectID" placeholder="请选择" style="width:80%">
              <el-option
                v-for="item in subjectList"
                :key="item.key"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标签名称" prop="tagName">
            <el-input v-model="form.tagName" style="width:80%" placeholder="请输入标签名称"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  name: 'tagsAdd',
  props: [
    // 'subjectList'
  ],
  created () {
  },
  data () {
    return {
      dialogFormVisible: false,
      hehe: true,
      subjectList: [],
      form: {
        tagName: '',
        subjectID: '',
        id: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '所属学科不能为空', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '标签名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹层显示
    add (row) {
      this.dialogFormVisible = true
      this.setupData()

      // 当DOM更新完后，vue会立刻马上调用该方法
      this.$nextTick(() => {
        // 清除校验结果
        this.$refs.dataForm.clearValidate()
      })

      this.form.tagName = row.tagName
      this.form.subjectID = row.subjectID
      this.form.id = row.id

      if (this.$route.query.id) {
        this.hehe = false
        this.form.subjectID = +this.$route.query.id
        console.log(this.form.subjectID)
      }
    },
    // 提交
    submit () {
      this.$refs.dataForm.validate(async flag => {
        if (!flag) return
        await update(this.form)
        // 1. 关闭弹窗
        this.dialogFormVisible = false
        // 2. 提示操作成功
        this.$message.success('操作成功！')
        // 3. 刷新组织架构数据
        this.$emit('refresh')
      })
    },
    // 所属学科简单列表
    setupData () {
      simple().then(data => {
        this.subjectList = data.data
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
